package com.marvels.approve.process.service.impl;

import com.marvels.approve.loan.dao.IMcamLoanApplyInfoDao;
import com.marvels.approve.loan.dto.McamLoanApplyInfoDTO;
import com.marvels.approve.process.dao.ProtocolTemplateInfoDao;
import com.marvels.approve.process.dto.ProtocolTemplateInfoDTO;
import com.marvels.approve.process.service.ProtocolTemplateInfoService;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.MarvelsLogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 合同模板复服务
 * 
 * @author 娄凯琦
 * @date 2018年9月27日
 */
@Service
public class ProtocolTemplateInfoServiceImpl implements ProtocolTemplateInfoService {

	@Autowired
	private IMcamLoanApplyInfoDao iMcamLoanApplyInfoDao;
	@Autowired
	private ProtocolTemplateInfoDao protocolTemplateInfoDao;

	/**
	 * 根据loanId查询签字模板
	 * 
	 * @author 娄凯琦
	 * @date 2018年9月27日
	 * @param loanId
	 * @return
	 * @throws MarvelsBusinessException
	 */
	@Override
	public List<ProtocolTemplateInfoDTO> queryTemplateInfoDTOByLoanId(String loanId) throws MarvelsBusinessException {
		if (loanId == null || "".equals(loanId)) {
			throw new MarvelsBusinessException("传入loanId参数为空!");
		}
		MarvelsLogUtil.getInstance()
				.info("ProtocolTemplateInfoServiceImpl queryTemplateInfoDTOByLoanId loanId =======" + loanId);
		McamLoanApplyInfoDTO mcamLoanApplyInfoResult = iMcamLoanApplyInfoDao.selectMcamLoanApplyInfoByLoanId(loanId);
		if (mcamLoanApplyInfoResult == null || mcamLoanApplyInfoResult.getFundCode() == null
				|| "".equals(mcamLoanApplyInfoResult.getFundCode())) {
			throw new MarvelsBusinessException("在McamLoanApplyInfo未查到对应的资金源loanId===" + loanId);
		}
		if (mcamLoanApplyInfoResult.getProductType() == null
				|| "".equals(mcamLoanApplyInfoResult.getProductType())) {
			throw new MarvelsBusinessException("在McamLoanApplyInfo未查到对应的产品大类loanId===" + loanId);
		}
		MarvelsLogUtil.getInstance()
				.info("ProtocolTemplateInfoServiceImpl queryTemplateInfoDTOByLoanId mcamLoanApplyInfoResult =======fundCode---"
						+ mcamLoanApplyInfoResult.getFundCode() + "----productType==="
						+ mcamLoanApplyInfoResult.getProductType());
		List<ProtocolTemplateInfoDTO> firstList = protocolTemplateInfoDao.selectByFundAndProductType(mcamLoanApplyInfoResult);
		if (firstList == null || firstList.size() == 0) {
			mcamLoanApplyInfoResult.setProductType(null);// 不用产品大类去筛选,因为有的产品没有特殊的附件需求,所以就用同一套
			firstList = protocolTemplateInfoDao.selectByFundAndProductType(mcamLoanApplyInfoResult);
		}
		return firstList;
	}

}
